<template>
  <div class="login-container">

    <div class="login-box">

      <login-header />

      <div class="title-box">
        <div>用户登录</div>
      </div>

      <el-form ref="postForm" :model="postForm" :rules="loginRules">
        <el-form-item prop="username">
          <el-input
            v-model="postForm.username"
            style="width: 100%"
            placeholder="用户名"
            prefix-icon="el-icon-user"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="postForm.password"
            show-password
            style="width: 100%"
            placeholder="密码"
            type="password"
            prefix-icon="el-icon-lock"
          />
        </el-form-item>

        <el-form-item prop="captchaValue">
          <yf-captcha v-model="postForm" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" :loading="loading" style="width: 100%" @click.native.prevent="accountLogin">登录</el-button>
        </el-form-item>

      </el-form>

      <div  v-if="siteData.functions.reg" style="display: flex; align-items: center; justify-content: flex-end">
        <el-link type="primary" href="/#/register">注册账号</el-link>
      </div>

    </div>

    <div class="footer" v-html="siteData.copyRight" />

  </div>

</template>

<script>
import { mapGetters } from 'vuex'
import YfCaptcha from '@/components/Captcha'
import LoginHeader from '@/views/login/components/login-header'

export default {
  components: { LoginHeader, YfCaptcha },
  data() {
    return {
      loading: false,
      postForm: {
        username: '',
        password: '',
        captchaKey: '',
        captchaValue: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
        captchaValue: [{ required: true, trigger: 'blur', message: '验证码不能为空' }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'siteData'
    ])
  },

  methods: {

    loginBack() {
      // 其它跳到后台
      this.$router.push({ path: '/admin/dashboard' })

      setTimeout(function() {
        this.loading = false
      }, 1800)
    },

    accountLogin() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.postForm)
            .then(() => {
              this.loginBack()
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "~@/styles/login.scss";

.el-divider__text{
  background: #f0f2f5;
  color: #999;
  font-size: 12px;
}
</style>
